#!/bin/bash
# Generates the GTLRService test support files.

set -e

# Where does this script live so we can hit things in the tree.
readonly TestsDir=$(dirname "$(echo $0 | sed -e "s,^\([^/]\),$(pwd)/\1,")")
readonly ScriptName=$(basename "$0")

readonly ServiceGeneratorDir="${TestsDir}/../Tools/ServiceGenerator"

readonly TMP_INSTALL_DIR=$(mktemp -dt "$ScriptName") || exit 1
trap "rm -rf $TMP_INSTALL_DIR" EXIT HUP INT QUIT TERM
readonly ServiceGeneratorBinary="${TMP_INSTALL_DIR}/usr/local/bin/ServiceGenerator"

# -----------------------------------------------------------------------------

# Helper to print a message and hopefully have it seen.
Banner() {
  echo ""
  echo "==============================================================================="
  echo "    $*"
  echo "==============================================================================="
  echo ""
}

# -----------------------------------------------------------------------------

Banner "Ensuring ServiceGenerator is up to date..."
/usr/bin/xcodebuild \
  -project "${ServiceGeneratorDir}/ServiceGenerator.xcodeproj" \
  -scheme ServiceGenerator \
  -configuration Debug \
  install \
  DSTROOT="${TMP_INSTALL_DIR}"

# -----------------------------------------------------------------------------

Banner "Running the ServiceGenerator..."
"${ServiceGeneratorBinary}" \
    --outputDir "${TestsDir}" \
    --addServiceNameDir yes \
    --removeUnknownFiles yes \
    --auditJSON \
    --brief \
    "${TestsDir}/TestingSvc.json"
